﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Security;
using System.Security.Cryptography;

namespace Business.用户身份
{
    public partial class 人员管理 : Form
    {
        public 人员管理()
        {
            InitializeComponent();
        }

        void inicbx()
        {
            lstUser.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} order by YHLB", "USERE");
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = dt.Rows[i]["YHLB"].ToString().Trim();
                li.SubItems.Add(dt.Rows[i]["NAME"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["JSLX"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["DrivingLicense"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["tizhong"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["SFZHM"].ToString().Trim());
                lstUser.Items.Add(li);
            }
        }
        private void 人员管理_Load(object sender, EventArgs e)
        {
            inicbx();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (edPwd1.Text != edPwd2.Text)
            {
                MessageBox.Show("密码不一致!", "提示", MessageBoxButtons.OK);
                return;
            }
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"Select count(*) From USERE where YHLB='{0}'", edUserID.Text);
            if (objquery.ExecuteS(ssqlex))
            {
                if (MessageBox.Show("已经存在该人员资料,是否修改?", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    return;
                }
                ssqlex = string.Format(@"update USERE set NAME='{0}',PASSWORD='{1}',JSLX='{2}',SFZHM='{4}',DrivingLicense='{5}',tizhong='{6}' where YHLB='{3}'", edUserName.Text, (edPwd1.Text), cbDrive.Text, edUserID.Text, txtsfz.Text,comboBox1.Text,
                    txttizhong.Text);
                if (!objquery.UpdateTable(ssqlex))
                {
                    MessageBox.Show("修改失败");
                }
            }
            else
            {
                if (MessageBox.Show("不存在该人员资料,是否添加?", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    return;
                ssqlex = string.Format(@"insert into USERE (YHJS,YHLB,NAME,PASSWORD,JSLX,MMYXQ,YHYXQ,SFZHM,DrivingLicense,tizhong) values ('系统管理','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", edUserID.Text, edUserName.Text, (edPwd1.Text), cbDrive.Text, DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.AddYears(20).ToString("yyyy-MM-dd HH:mm:ss"), txtsfz.Text, comboBox1.Text,
                    txttizhong.Text);
                if (!objquery.UpdateTable(ssqlex))
                {
                    MessageBox.Show("添加失败");
                }
            }
            inicbx();
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"delete USERE where YHLB='{0}'", edUserID.Text);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("删除失败");
            }
            inicbx();
        }

        private void lstUser_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (ListViewItem item in lstUser.SelectedItems)
            {
                edUserID.Text = item.SubItems[0].Text.ToString().Trim();
                edUserName.Text = item.SubItems[1].Text.ToString().Trim();
                cbDrive.Text = item.SubItems[2].Text.ToString().Trim();
                comboBox1.Text= item.SubItems[3].Text.ToString().Trim();
                txttizhong.Text = item.SubItems[4].Text.ToString().Trim();
                txtsfz.Text = item.SubItems[5].Text.ToString().Trim();
            }
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"Select * From USERE where YHLB='{0}'", edUserID.Text);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < 1; i++)
            {
                edUserID.Text = dt.Rows[i]["YHLB"].ToString().Trim();
                edUserName.Text = dt.Rows[i]["NAME"].ToString().Trim();
                edPwd1.Text = (dt.Rows[i]["PASSWORD"].ToString().Trim());
                edPwd2.Text = (dt.Rows[i]["PASSWORD"].ToString().Trim());
                cbDrive.Text = dt.Rows[i]["JSLX"].ToString().Trim();
                txtsfz.Text = dt.Rows[i]["SFZHM"].ToString().Trim();
                comboBox1.Text = dt.Rows[i]["DrivingLicense"].ToString().Trim();
                txttizhong.Text = dt.Rows[i]["tizhong"].ToString().Trim();
            }
        }

    }
}
